package com.juzhencms.apps.busi.service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Service;

import com.juzhencms.apps.base.common.config.Config;
import com.juzhencms.apps.base.common.database.DBTable;

@Service
public class AdService extends CommonService{

	

	//获得图片广告
	
	public String getImgAd(int spaceId, HttpServletRequest request) {
		List adList=this.getAdList(spaceId,1,request);
		if(adList==null || adList.size()==0) {
			return "";
		}
		Map ad=(Map)adList.get(0);
		String html="<img width='"+ad.get("width")+"' height='"+ad.get("height")+"' src='"+ad.get("image")+"'/>";
			
		if(ad.get("link_url")!=null && !ad.get("link_url").equals("")){
			html="<a href='"+ad.get("link_url")+"' target='_blank'>"+html+"</a>";
		}
		return html;
		
		
	}
	//获取轮播广告
	//
	public List getAdList(int spaceId,int num,HttpServletRequest request) {
		
		//获取广告位（宽度及高度信息）
		Map space=this.getJdbcDao().queryRow("select * from "+DBTable.adSpace+" where id=?", new Object[] {spaceId});
		
		List adList=null;
		if(space!=null) {
			int menuCase=(int)space.get("menu_case");
			if(menuCase==0) {
				adList=this.getJdbcDao().querySql("select * from "+DBTable.ad+" where space_id=? and publish=1 and del_flag=0 order by position asc,id asc limit 0,?",  new Object[] {spaceId,num});
			}else {
				
				List locationMenus=(List)request.getAttribute("locationMenuList");
				if(locationMenus!=null) {
					for(int i=locationMenus.size()-1;i>=0;i--) {
						Map tmpMenu=(Map)locationMenus.get(i);
						adList=this.getJdbcDao().querySql("select * from "+DBTable.ad+" where space_id=? and menu_id=? and publish=1 and del_flag=0 order by position asc,id asc limit 0,?",  new Object[] {spaceId,tmpMenu.get("id"),num});
						if(adList!=null && adList.size()>0)
							break;
					}
				}
			}
		}
		
		if(adList!=null && adList.size()>0) {
			for(int i=0;i<adList.size();i++) {
				Map ad=(Map)adList.get(i);
				ad.put("width", space.get("width"));
				ad.put("height", space.get("height"));
				
				if(ad.get("image")==null || ad.get("image").equals("")) {
					ad.put("image","nophoto.jpg");
				}
				ad.put("image", this.getConfig().getUploadUrlPath()+"/"+ad.get("image"));	
			}
		}
		
		
		
		return adList;
	}

	
	public String getImgAd(String flag, HttpServletRequest request) {
		Map site=(Map)request.getAttribute("localSite");
		if(site!=null && site.get("id")!=null){
			Map space=this.getJdbcDao().queryRow("select id from "+DBTable.adSpace+" where site_id=? and tpl_flag=? order by id asc limit 0,1", new Object[]{site.get("id"),flag});
			if(space!=null){
				return this.getImgAd(new Integer(space.get("id").toString()), request);
			}
		}
		return null;
	}

	
	public List getAdList(String flag, int num, HttpServletRequest request) {
		Map site=(Map)request.getAttribute("localSite");
		if(site!=null && site.get("id")!=null){
			Map space=this.getJdbcDao().queryRow("select id from "+DBTable.adSpace+" where site_id=? and tpl_flag=? order by id asc limit 0,1", new Object[]{site.get("id"),flag});
			if(space!=null){
				return this.getAdList(new Integer(space.get("id").toString()),num, request);
			}
		}
		return null;
	}
	
}
